package com.polytech.beans;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

import com.polytech.business.Request;

public class ListCars<T> extends LinkedList<Car> {

	private Collection<Car> cars;
	private Request request;
	LinkedList<Car> sortedCars=new LinkedList();
	public ListCars(Request request,Collection<Car> cars) {
		this.cars=cars;
		this.request=request;
		sort();
	}
	
	private void sort(){
		
		int tabDistance[]=new int[cars.size()];
		int i=0;
		for (Car car: cars) {
			
			tabDistance[i]=(int)car.distance(request);
			i++;
		}
		
		Object t[]= cars.toArray();
		for (int j = 0; j < tabDistance.length; j++) {
			int min=j;
			for (int j2 = j+1; j2 < tabDistance.length; j2++) {
				if(tabDistance[min]>tabDistance[j2])
					min=j2;
			
			}
			
			
			Object cartmp=t[min];
			t[min]=t[j];
			t[j]=cartmp;
			
			int tmp=tabDistance[min];
			tabDistance[min]=tabDistance[j];
			tabDistance[j]=tmp;
			
			System.out.println(">> " +tabDistance[j]);
			sortedCars.add((Car)t[j]);
			
			System.out.println(">> "+tabDistance[j]);
			
		}
		
		
		
	}
	
	public List<Car> getCarsSoted(int nb){
		
		if(nb>sortedCars.size())
			return sortedCars;
		else
			return sortedCars.subList(0, nb);
		
	}
	
	
}
